package com.foreknow.demo11;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * set集合
 */
public class SetDemo {
    public static void main(String[] args) {
        //创建一个Set集合
        Person p1 = new Person();
        p1.setAge(1);
        Person p2 = new Person();
        p2.setAge(2);
        Person p3 = new Person();
        p3.setAge(3);
        Set<Person> set = new HashSet<>();
        //向Set集合中添加元素
        set.add(p1);
        set.add(p2);
        set.add(p3);
        //获取Set集合中所有的元素
        Iterator<Person> it = set.iterator();
        while (it.hasNext()){
            Person p = it.next();
            System.out.println(p.getAge());
        }
    }
}
